package com.maddox.relax.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * @author Maddox
 * 联系人索引列表
 */
public class SlideBar extends View {
    //展示的索引内容
    public static String[] indexStrs = {"*", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};
    //选中
    int choose = -1;
    Paint paintText;
    Paint paintChoose;
    Paint paintBg;
    //索引选中提示框
    private TextView textDialog;
    //索引文本颜色
    private int textColor = Color.BLACK;
    //索引选中背景颜色
    private int bgColor = Color.GREEN;

    public SlideBar(Context context) {
        this(context, null);
    }

    public SlideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paintText = new Paint();
        paintText.setColor(textColor);
        paintText.setTypeface(Typeface.DEFAULT_BOLD);
        paintText.setAntiAlias(true);
        paintText.setTextSize(20);
        paintBg = new Paint();
        paintBg.setColor(bgColor);
        paintChoose = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintChoose.setColor(Color.WHITE);
        paintChoose.setFakeBoldText(true);

    }

    public void setTextDialog(TextView textView) {
        this.textDialog = textView;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获取当前View宽高
        int width = getWidth();
        int height = getHeight();
        //获取每个字母的高度
        int letterHeight = height / indexStrs.length;

        for (int i = 0; i < indexStrs.length; i++) {
            //每个索引字母的绘制x点=当前View宽度的一半减去绘制字母的宽度的一半
            float xPos = width / 2 - paintText.measureText(indexStrs[i]) / 2;
            float yPos = letterHeight * i + letterHeight;
            //选中的状态绘制
            if (i == choose) {
                canvas.drawCircle(width / 2, yPos - paintText.measureText(indexStrs[i]) / 2, width / 3, paintBg);
                canvas.drawText(indexStrs[i], xPos, yPos, paintChoose);
            }else{
                canvas.drawText(indexStrs[i], xPos, yPos, paintText);
            }

        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();//点击的y坐标
        int oldChoose = choose;
        //点击y坐标所占总高度的比例*数组的长度就是点击到的字母角标
        int c = (int) (y/getHeight()*indexStrs.length);
        switch (action){
            case MotionEvent.ACTION_UP:
                choose=-1;
                invalidate();
                if (textDialog!=null){
                    textDialog.setVisibility(INVISIBLE);
                }
                break;

            default:
                if (oldChoose!=c){
                    if (c>0&&c<indexStrs.length){
                        if (touchLetterListener!=null){
                            touchLetterListener.onTouchingLetterChanged(indexStrs[c]);
                        }
                        if (textDialog!=null){
                            textDialog.setText(indexStrs[c]);
                            textDialog.setVisibility(VISIBLE);
                        }
                        choose=c;
                        invalidate();
                    }
                }

                break;
        }




        return true;
    }

    private OnTouchingLetterListener touchLetterListener;

    public void setTouchLetterListener(OnTouchingLetterListener touchLetterListener) {
        this.touchLetterListener = touchLetterListener;
    }

    public interface OnTouchingLetterListener{
       void onTouchingLetterChanged(String letter);
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}
